package org.springcloud.wisdom.springcloud_03_freshman.admin.dao;

import org.apache.ibatis.annotations.*;
import org.springcloud.wisdom.springcloud_03_freshman.admin.entity.Notice;

import java.util.List;

@Mapper
public interface NoticeDao {

    //发布公告
    @Insert("insert into notice (title,content,identity,type,publishdt,ispublic) values " +
            "(#{title},#{content},#{identity},#{type},#{publishdt},#{ispublic})")
    public int addNotice(Notice notice);
    //审核公告
    @Update("update notice set publishdt=#{publishdt},ispublic=#{ispublic} where id =#{id}")
    public int updateNotice(Notice notice);
    //删除公告(逻辑上的删除)
    @Update("update notice set isdel=1 where id=#{id}")
    public int deleteNotice(Integer id);
    //查看一条公告
    @Select("select * from notice where id=#{id}")
    public Notice getNoticeById(Integer id);
    //查看公告
    @Select("<script>" +
            "select * from notice" +
            "  <where>" +
            "    <if test=\"ispublic != null\">" +
            "    and ispublic = #{ispublic} " +
            "    </if>" +
            "  isdel = 0 " +
            "    <if test=\" type != null\">" +
            " <bind name=\"Key\" value=\"type+'%'\"/>"+
            "    and    type like #{Key} " +
            "    </if>" +
            "</where>"+
            " <if test=\"orderBy!= null\">" +
            "ORDER BY" +
            "  CASE " +
            "    WHEN ispublic = '待审核' THEN 1" +
            "    WHEN ispublic = '同意' THEN 2" +
            "    when ispublic = '拒绝' THEN 3" +
            "  ELSE 4" +
            "  END," +
            "  ispublic ASC " +
            "  </if> " +
            "limit #{start},#{size}"+
            "</script>")
    public List<Notice> getNotices(int start,int size,String ispublic,Integer type,Integer orderBy);
    @Select("<script>" +
                "select count(*) from notice" +
                "  <where>" +
                "    <if test=\"ispublic != null\">" +
                "   and  ispublic = #{ispublic} " +
                "    </if>" +
                "    isdel = 0 " +
                "    <if test=\" type != null\">" +
                " <bind name=\"Key\" value=\"type+'%'\"/>"+
                "    and    type like #{Key} " +
                "    </if>" +
                "</where>"+
                "</script>")
    public int countNotice(String ispublic,Integer type);

    @Delete("<script>" +
            "  delete from notice where ID in" +
            "  <foreach collection=\"ids\" item=\"did\" open=\"(\" close=\")\" separator=\",\">" +
            "    #{did}" +
            "   </foreach>" +
            "</script>")
    public int delbyIds(List<Integer> ids);
}
